package cn.com.api.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiT_member;
import cn.com.api.service.ApiT_memberService;
import cn.com.util.*;
import cn.com.weibo4j.Oauth;
import cn.com.weibo4j.Users;
import cn.com.weibo4j.http.AccessToken;
import cn.com.weibo4j.model.User;
import cn.com.weibo4j.model.WeiboException;

@Controller
@RequestMapping(Utils.SYS_API + "weibo4j/")
public class Weibo4jController {

	private static Logger logger = Logger.getLogger(Weibo4jController.class);

	@Autowired
	private ApiT_memberService apiT_memberService;

	/**
	 * 获取微博用户的信息
	 * 
	 */
	@RequestMapping("/getWeibo4jUserInfo")
	public void getWechatUserInfo(HttpServletRequest request, HttpServletResponse response, String code)
			throws IOException {
		Map<String, Object> map = new HashMap<String, Object>();
		Oauth oauth = new Oauth();

		try {
			AccessToken accessToken = oauth.getAccessTokenByCode(code);
			// System.out.println(accessTokenByCode2);
			Users um = new Users(accessToken.getAccessToken());

			User user = um.showUserById(accessToken.getUid());
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作完成");
			map.put(Utils.DATAS, user);

		} catch (WeiboException e) {
			if (401 == e.getStatusCode()) {
				// Log.logInfo("Unable to get the access token.");
			} else {
				e.printStackTrace();
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_NO_DATA);
			map.put(Utils.MESSAGE, "没有获取到用户信息！");
		}
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

}
